package com.nchu.student_score.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nchu.student_score.dataObject.StudentResult;
import com.nchu.student_score.mapper.GradeTempMapper;
import com.nchu.student_score.mapper.StudentMapper;
import com.nchu.student_score.mapper.TeachPersonalMapper;
import com.nchu.student_score.model.GradeTemp;
import com.nchu.student_score.model.Student;
import com.nchu.student_score.model.TeachPersonal;
import com.nchu.student_score.service.StudentService;
import com.nchu.student_score.util.Define;
import com.nchu.student_score.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;
import java.util.Objects;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    StudentMapper studentMapper;
    @Autowired
    GradeTempMapper gradeTempMapper;
    @Autowired
    TeachPersonalMapper teachPersonalMapper;

    @Override
    public ResultVo<Student> updateStudent(Student input, Integer oldStuId) {
        ResultVo<Student> resultVo = new ResultVo<>();

        if(oldStuId == -1){//新增学生
            Student student = studentMapper.selectByPrimaryKey(input.getStuId());
            if(student != null){
                resultVo.setStatus(1);
                resultVo.setMessage("目标学号已存在!");
                return resultVo;
            }

            input.setStuPassword(this.getInitPwd(input));
            studentMapper.insert(input);
            resultVo.setStatus(0);
            resultVo.setMessage("新增学生 " + input.getStuId() + " " + input.getStuName() + " 成功!");
        }
        else{//修改学生
            Student oldStudent = studentMapper.selectByPrimaryKey(oldStuId);
            if(oldStudent == null){
                resultVo.setStatus(1);
                resultVo.setMessage("原学号未找到!");
                return resultVo;
            }

            if(! Objects.equals(input.getStuId(), oldStuId)){//需要修改学号
                Student student = studentMapper.selectByPrimaryKey(input.getStuId());
                if(student != null){
                    resultVo.setStatus(2);
                    resultVo.setMessage("目标学号已存在!");
                    return resultVo;
                }
                //查询原学号是否已被使用
                List<GradeTemp> gradeTempList = gradeTempMapper.selectByStuId(oldStuId);
                List<TeachPersonal> teachPersonalList = teachPersonalMapper.selectByStuId(oldStuId);
                if(gradeTempList.size() > 0 || teachPersonalList.size() > 0){
                    resultVo.setStatus(3);
                    resultVo.setMessage("原学号当前正在使用中,不能修改学号!");
                    return resultVo;
                }
                else{
                    input.setStuPassword(oldStudent.getStuPassword());
                    studentMapper.deleteByPrimaryKey(oldStuId);
                    studentMapper.insert(input);
                }
            }
            else{//无需修改学号
                input.setStuPassword(oldStudent.getStuPassword());
                studentMapper.updateByPrimaryKey(input);
            }
            resultVo.setStatus(0);
            resultVo.setMessage("修改学生成功!");
        }

        return resultVo;
    }

    @Override
    public PageInfo<StudentResult> getStudentList(StudentResult input, Integer currPage) {
        if(currPage == null) currPage = 1;
        PageHelper.startPage(currPage, Define.PAGE_SIZE);
        return new PageInfo<>(studentMapper.findStudents(input));
    }

    @Override
    public ResultVo<Student> initStudentPwd(Student student) {
        ResultVo<Student> resultVo = new ResultVo<>();
        student.setStuPassword(getInitPwd(student));

        studentMapper.updateByPrimaryKey(student);
        resultVo.setStatus(0);
        resultVo.setMessage("学生”" + student.getStuId() + " - "
                + student.getStuName() + "”的密码重置成功!");

        return resultVo;
    }

    private String getInitPwd(Student student){//获得初始密码
        String idCard = student.getStuIdCard();
        String newPwd = "" + student.getStuId() + idCard.substring(12,18);//密码为id+身份证号后6位
        return DigestUtils.md5DigestAsHex(newPwd.getBytes());
    }

    @Override
    public ResultVo<StudentResult> findStudent(Integer stuId) {
        ResultVo<StudentResult> resultVo = new ResultVo<>();
        if(stuId == null || stuId < 100){
            resultVo.setStatus(1);
            resultVo.setMessage("您输入的待搜索信息太少了!");
            return resultVo;
        }

        StudentResult studentResult = new StudentResult();
        studentResult.setStuId(stuId);
        resultVo.setList(studentMapper.findStudents(studentResult));
        resultVo.setStatus(0);
        return resultVo;
    }
}
